package com.shuoyue.ycgk.views.dialog;

import android.app.Dialog;
import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.utils.SizeUtil;
import com.shuoyue.ycgk.utils.XToast;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * 带确认取消按钮的Dialog
 * Created by Forty'7 on 2019/6/17.
 */

public class SetDayTagetDialog extends Dialog {
    @BindView(R.id.time)
    EditText time;
    @BindView(R.id.question_num)
    EditText questionNum;
    @BindView(R.id.scro_rate)
    EditText scroRate;
    private Context mContext;

    SetTagetCallBack callBack;

    public SetDayTagetDialog(@NonNull Context context, SetTagetCallBack callBack) {
        super(context, R.style.dialog);
        this.mContext = context;
        this.callBack = callBack;
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = View.inflate(context, R.layout.dialog_set_day_taget, null);
        setContentView(view);
        ButterKnife.bind(this, view);
        getWindow().setLayout((SizeUtil.getScreenWidth(mContext) * 3) / 4, ViewGroup.LayoutParams.WRAP_CONTENT);
        getWindow().setGravity(Gravity.CENTER);

        scroRate.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() == 0) {
                    return;
                }
                Integer i = Integer.valueOf(s.toString());
                if (i > 100) {
                    scroRate.setText("100");
                }
            }
        });
    }


    @OnClick({R.id.tv_cancel, R.id.tv_ok})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_cancel:
                dismiss();
                break;
            case R.id.tv_ok:
                String timeStr = time.getText().toString();
                String questionNumStr = questionNum.getText().toString();
                String scroRateStr = scroRate.getText().toString();
                if (TextUtils.isEmpty(timeStr)) {
                    XToast.toast(mContext, "请输入目标分钟数");
                }
                if (TextUtils.isEmpty(questionNumStr)) {
                    XToast.toast(mContext, "请输入目标做题量");
                }
                if (TextUtils.isEmpty(scroRateStr)) {
                    XToast.toast(mContext, "请输入目标正确率");
                    return;
                }
                if (callBack != null) {
                    callBack.setTaget(Integer.valueOf(timeStr), Integer.valueOf(questionNumStr), Integer.valueOf(scroRateStr));
                    dismiss();
                }


                break;
        }
    }

    public void setInit(int timeValue, int questionNumValue, int scroRateValue) {
        time.setText("" + timeValue);
        questionNum.setText("" + questionNumValue);
        scroRate.setText("" + scroRateValue);

    }

    public interface SetTagetCallBack {
        void setTaget(int minut, int questionNum, int scoreRate);
    }
}
